package com.beidouin.GnssMain.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.View;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Utils {
	public static String port = "dev/ttyAMA3";
	public static String path = "/sdcard/flaims/";
	/**
     * 十六进制字符串装十进制
     * 
     * @param hex
     *            十六进制字符串
     * @return 十进制数值
     */
    public static int hexStringToAlgorism(String hex) {
        hex = hex.toUpperCase();
        int max = hex.length();
        int result = 0;
        for (int i = max; i > 0; i--) {
            char c = hex.charAt(i - 1);
            int algorism = 0;
            if (c >= '0' && c <= '9') {
                algorism = c - '0';
            } else {
                algorism = c - 55;
            }
            result += Math.pow(16, max - i) * algorism;
        }
        return result;
    }
	/**
     * 数字字符串转ASCII码字符串
     * 
     * @return ASCII字符串
     */
    public static String StringToAsciiString(String content) {
        String result = "";
        int max = content.length();
        for (int i = 0; i < max; i++) {
            char c = content.charAt(i);
            String b = Integer.toHexString(c);
            result = result + b ;
        }
        return result;
    }
    /**
     * 将十进制转换为指定长度的十六进制字符串
     * 
     * @param algorism
     *            int 十进制数字
     * @param maxLength
     *            int 转换后的十六进制字符串长度
     * @return String 转换后的十六进制字符串
     */
    public static String algorismToHEXString(int algorism, int maxLength) {
        String result = "";
        result = Integer.toHexString(algorism);

        if (result.length() % 2 == 1) {
            result = "0" + result;
        }
        return patchHexString(result.toUpperCase(), maxLength);
    }
    /**
     * HEX字符串前补0，主要用于长度位数不足。
     * 
     * @param str
     *            String 需要补充长度的十六进制字符串
     * @param maxLength
     *            int 补充后十六进制字符串的长度
     * @return 补充结果
     */
    static public String patchHexString(String str, int maxLength) {
        String temp = "";
        for (int i = 0; i < maxLength - str.length(); i++) {
            temp = "0" + temp;
        }
        str = (temp + str).substring(0, maxLength);
        return str;
    }
    /**
     * ASCII码字符串转数字字符串
     * 
     * @param String
     *            ASCII字符串
     * @return 字符串
     */
    public static String AsciiStringToString(String content) {
        String result = "";
        int length = content.length() / 2;
        for (int i = 0; i < length; i++) {
            String c = content.substring(i * 2, i * 2 + 2);
            int a = hexStringToAlgorism(c);
            char b = (char) a;
            String d = String.valueOf(b);
            result += d;
        }
        return result;
    }
    final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
    public static String bytesToHex(byte bytes) {
        char[] hexChars = new char[2];
//        for ( int j = 0; j < 1; j++ ) {
            int v = bytes & 0xFF;
            hexChars[0] = hexArray[v >>> 4];
            hexChars[1] = hexArray[v & 0x0F];
//        }
        return new String(hexChars);
    }
    public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len-1; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                                 + Character.digit(s.charAt(i+1), 16));
        }
        return data;
    }
    public static Bitmap loadBitmapFromView(Context context, View view) {

	    Bitmap bitmap;

	    BitmapFactory.Options options = new BitmapFactory.Options();
	    options.inPurgeable = true;
	    options.inInputShareable = true;
	     bitmap = Bitmap.createBitmap(view.getWidth(),
	    		 view.getHeight(), Bitmap.Config.ARGB_4444);
	    Canvas c = new Canvas(bitmap);
	    view.layout(0, 0, view.getWidth(), view.getHeight());
	    view.draw(c);
	    c = null;

	    return bitmap;
	}
    public static int countOccrrence(String raw, String pat){
        Pattern pattern = Pattern.compile(pat); //case insensitive, use [g] for only lower
        Matcher matcher = pattern.matcher(raw);
        int count = 0;
        while (matcher.find()) count++;
        return count;
    }

    public static int countOccrrence(String raw, String pat,String pat2){
        Pattern pattern = Pattern.compile(pat); //case insensitive, use [g] for only lower
        Pattern pattern2 = Pattern.compile(pat2); //case insensitive, use [g] for only lower
        Matcher matcher = pattern.matcher(raw);
        Matcher matcher2 = pattern2.matcher(raw);
        int count = 0;
        while (matcher.find()) count++;
        while (matcher2.find()) count++;
        return count;
    }

    public static boolean isNumeric(String str)
    {
        return str.matches("-?\\d+(\\.\\d+)?");  //match a number with optional '-' and decimal.
    }

    public static String formatTime(long times){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
        Date date = new Date(times);
        String tim = sdf.format(date);
        return tim;
    }
}
